﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    private const string STRING_USERNAME_LOGIN = "BUS:COOKIES:LOGIN";
    private const string STRING_PASSWORD_LOGIN = "BUS:COOKIES:PASSWORD";
    private ManageUsers mnUsers;

    protected void Page_Load(object sender, EventArgs e)
    {
        mnUsers = new ManageUsers();
        if (Request.Cookies[STRING_USERNAME_LOGIN] != null && Request.Cookies[STRING_PASSWORD_LOGIN] != null)
        {
            string username = Request.Cookies[STRING_USERNAME_LOGIN].Value;
            string password = Request.Cookies[STRING_PASSWORD_LOGIN].Value;
            BehindCheckLogin(username, password);
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtusername.Text.Trim().Length == 0 || txtpassword.Text.Trim().Length == 0)
        {
            lblResult.Text = "Tài khoản và mật khẩu trống!";
            lblResult.Visible = true;
            return;
        }

        if (chkRemember.Checked)
        {
            Response.Cookies[STRING_USERNAME_LOGIN].Expires = DateTime.Now.AddDays(30);
            Response.Cookies[STRING_PASSWORD_LOGIN].Expires = DateTime.Now.AddDays(30);
        }
        else
        {
            Response.Cookies[STRING_USERNAME_LOGIN].Expires = DateTime.Now.AddDays(-1);
            Response.Cookies[STRING_PASSWORD_LOGIN].Expires = DateTime.Now.AddDays(-1);
        }

        Response.Cookies[STRING_USERNAME_LOGIN].Value = txtusername.Text.Trim();
        Response.Cookies[STRING_PASSWORD_LOGIN].Value = txtpassword.Text.Trim();

        int results = mnUsers.loginSystem(txtusername.Text.Trim(), txtpassword.Text.Trim());
        if (results <= 0)
        {
            lblResult.Text = "Đăng nhập thất bại!";
            lblResult.Visible = true;
            return;
        }
        else if (results == 1)
        {
            Session["Login"] = "admin" + ":" + Response.Cookies[STRING_USERNAME_LOGIN].Value;
            Response.Redirect("View/ManageUsers.aspx");
        }
        else if (results == 2)
        {
            Session["Login"] = "user" + ":" + Response.Cookies[STRING_USERNAME_LOGIN].Value;
            Response.Redirect("Default.aspx");
        }
    }

    public void BehindCheckLogin(string username, string password)
    {
        int results = mnUsers.loginSystem(txtusername.Text.Trim(), txtpassword.Text.Trim());
        if (results <= 0)
        {
            lblResult.Text = "Đăng nhập thất bại!";
            lblResult.Visible = true;
            return;
        }
        else if (results == 1)
        {
            Session["Login"] = "admin" + ":" + Response.Cookies[STRING_USERNAME_LOGIN].Value;
            Response.Redirect("Default.aspx");
        }
        else if (results == 2)
        {
            Session["Login"] = "admin" + ":" + Response.Cookies[STRING_USERNAME_LOGIN].Value;
            Response.Redirect("View/ManageUsers.aspx");
        }
    }
}